package com.imcrl.piano.core
{
	import com.imcrl.piano.utils.SpriteUtils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class NumberAssets extends BitmapAssets
	{
		private var numberView:DisplayObjectContainer;
		public function NumberAssets()
		{
			super(30);
			
			var ass:Sprite=new Assets.NumberSkin();
			
			var bitmapdata:BitmapData=new BitmapData(ass.width,ass.height,true,0);
			bitmapdata.draw(ass);
			this.setAsset(bitmapdata);
		}
		
		public function getView():DisplayObject{
			if(numberView==null)
			{
				numberView=SpriteUtils.createDisplayContainer(false);
			}
			return numberView;
		}
		
		
		public function viewNumber(value:int):void{
			if(numberView==null){
				getView();
			}else{
				SpriteUtils.clear(numberView)
			}
			
			
			var str:String=value.toString();
			
			var len:int=str.length;
			
			var num:int;
			var bitmap:Bitmap;
			
			for (var i:int=0;i<len;i++){
				
				num=int(str.charAt(i));
				
				bitmap=new Bitmap(get(num));
							
				bitmap.x=numberView.width;
				
				numberView.addChild(bitmap);
				
			}
			
			numberView.x -=(numberView.width>>1);
		}
		
		
		override protected function spliceAsset():void{
			
			var col:int=10;
			var row:int=3;
			
			var sw:int=source.width/col;
			var sh:int=source.height/row;
			
			
			var bitmapdata:BitmapData;
			var rect:Rectangle=new Rectangle(0,0,sw,sh);
			var p:Point=new Point();
			
			for(var i:int=0;i<3;i++){
				
				for (var j:int=0;j<10;j++){
					
					rect.y=i*sh;
					rect.x=j*sw;
					
					bitmapdata=new BitmapData(sw,sh,true,0);
					bitmapdata.copyPixels(source,rect,p);
					
					assets.push(bitmapdata);
				}
			}
		}
	}
}